const WebSocket = require('ws');

function main() {
    LoginCheckin()
    // 创建WebSocket连接
    const ws = new WebSocket('wss://agnj3.bong88.com/socket.io/?gid=8804c90695104ab8&token=9519b50a-4cd0-4565-a6d0-8fca90001866&id=237280431&rid=0&EIO=3&transport=websocket');
    ws.on('open', function open() {
        console.log('WebSocket连接已建立');
    });
    ws.on('message', function incoming(data) {
        // 将data转换为字符串 
        let d = data.toString();
        console.log(new Date() + ' | 收到消息:', d);
        if (d === '40') {
            ws.send(`42["init",{"gid":"2e32cd843ce22e4d","token":"9519b50a-4cd0-4565-a6d0-8fca90001866","id":"237280431","rid":"0","v":2}]`)
        }
        if (d.indexOf("pnid") > 0) {
            ws.send(`42["subscribe",[["spread",[{"id":"c0","rev":"","sorting":0,"condition":{}}]],["odds",[{"id":"c1","rev":"","sorting":0,"condition":{"sporttype":1,"no_stream":true,"source":"hotleaguewall","mini":1,"bettype":[1,3]}}]]]]`)
            setTimeout(() => {
                ws.send(`42["subscribe",[["streaming",[{"id":"c2","rev":"","sorting":0,"condition":{"sporttype":1}}]]]]`)
            }, 1000);
        }
    });
    ws.on('close', function close() {
        console.log('WebSocket连接已关闭');
    });
    ws.on('error', function error(err) {
        console.error('WebSocket错误:', err);
    });

    // 定时8秒发送一次消息
    setInterval(() => {
        if (ws.readyState === WebSocket.OPEN) {
            ws.send('2');
            console.log('发送心跳消息');
        }
    }, 8000);
}


function LoginCheckin(rt = "", at = "") {
    fetch("https://d.bong88.com/LoginCheckin/Index", {
        "headers": {
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
            "baggage": "sentry-environment=production,sentry-release=20250801164008,sentry-public_key=fa65784e932647a2cda894cecf31b824,sentry-trace_id=728aeb00a2944f008c4806b16038c633,sentry-sampled=false,sentry-sample_rand=0.990712937454378,sentry-sample_rate=0.1",
            "cache-control": "no-cache",
            "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
            "custid": "237280431",
            "devicetype": "1",
            "pragma": "no-cache",
            "priority": "u=1, i",
            "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "sentry-trace": "728aeb00a2944f008c4806b16038c633-a11022aa170bb646-0",
            "username": "QZ9QAG01333",
            "x-requested-with": "XMLHttpRequest",
            "cookie": "_ga=GA1.1.766635014.1753883585; LOGIN_PLATFORM=desktop; gesTip=1; rememberMe=true; UserName=QZ9QAG01333; LangKey=en; LANGUAGE_I18N=en; gfp=4367509dfd0a6497e4988030cb984efc7e15fff9a6c6b16ec207e6785d35d948; _clck=1cwootj%7C2%7Cfy4%7C0%7C2037; _culture=en-US; Web2Session=CfDJ8C7cZU%2FVlf9Fi1oG1luCKBTKb9ZHwFBOoP%2FHGL2d8nkcabySST3hP8hNVZTWdu65fF44YzO%2BefvFeYuQE3%2BwVe1pxx71cl9XU1E9EelZeBtmxyzoLMmNDje1C%2F9GGMsHObWD9c0dNmufvNEiB%2BNVhRgGaHB9v0wrUG6%2FIKYZ1ha%2B; dct=5c0e68958b37c07d3e8e8f5c8a7be672; _clsk=1si3fmy%7C1754152350416%7C6%7C1%7Cj.clarity.ms%2Fcollect; _ga_FR64WVHR9J=GS2.1.s1754149572$o11$g1$t1754152409$j60$l0$h0; _ga_4626WYQGJD=GS2.1.s1754149577$o6$g1$t1754153917$j14$l0$h0; lip=wrrCtcKUwoTCkMOawovChsKjw77DhMOwB8Kbw5jCmcO7ZG1Rw5jCmcO0wr3DtR5VNljDt0jCsUNgZsO8wpDDmTMBw53DskvCiEh/T8OYUcOz; __cf_bm=QhCtKgLtFc1GfzQhiaahV5AgjN6MNP1hQqjfAIP2zbI-1754155357-1.0.1.1-BCMk57HqgQ14veXKl4Qad4_7aBy2RgEk.4bz3FH0d7ULe0i3KSrClPGnfrH_hOjY0O2FSiU1JT.eRuRDcLUR6gpGG6vasm0cE5i3CM_JRLs",
            "Referer": "https://d.bong88.com/Sports/"
        },
        "body": `rt=${rt}&at=${at}`,
        "method": "POST"
    }).then(response => {
        return response.json();
    }).then(response => {
        console.log("签到成功", response.data);
        let data = response;
        setTimeout(() => {
            LoginCheckin(data.Data.rt, data.Data.at)
        }, 1000 * 60);
    }).catch(error => {
        console.error("签到失败", error);
    });
}

main()